/*
Copyright 2010 SGSL

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


package com.dataiti.sgsl.layer;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import com.jhlabs.map.ProjectionPainter;
import com.jhlabs.map.layer.Layer;
import com.jhlabs.map.layer.MapGraphics;
import com.jhlabs.map.proj.Projection;


/**
 * EditLayer defines the Circle,Line,FilledCircle,Arc over the globe
 * This class extends Layer interface. 
 */
public class Editlayer extends Layer {
	
	private String type;
	private float startLongtitude;
	private float startLatitude;
	private float endLongtitude;
	private float endLatitude;
	private float radius;	
	private Color objColor;
	
	
	/**
	 * This constructor initialize start latitude,longtitude,radius,type and color. This constructor 
	 * is mainly used for drawing circle,filled circle,circle. 
	 * @param startLongtitude - Starting longtitude point in degree
	 * @param startLatitude - Starting latitude point in degree
	 * @param radius2 - radius of the circle 
	 * @param type - It is a string constants which take line,circle,point,filled circle
	 * @param objColor - It a object of type Color
	 */
	public Editlayer(float startLongtitude,float startLatitude,double radius2,String type,Color objColor)
	{
		this.startLatitude = startLatitude;
		this.startLongtitude = startLongtitude;
		this.radius = (float) radius2;	
		this.type = type;
		this.objColor = objColor;		
	}
	
	/**
	 * This constructor initialize start latitude,longtitude,end latitude,longtitude,radius,type and color. This constructor 
	 * is mainly used for drawing circle,filled circle,circle. 
	 * @param startLongtitude - Starting longtitude point in degree
	 * @param startLatitude - Starting latitude point in degree
	 * @param radius2 - radius of the circle 
	 * @param type - It is a string constants which take line,circle,point,filled circle
	 * @param objColor - It a object of type Color
	 */
	public Editlayer(float startLongtitude,float startLatitude,float endLongtitude,float endLatitude,String type,Color objColor)
	{
		this.startLatitude = startLatitude;
		this.startLongtitude = startLongtitude;
		this.endLatitude = endLatitude;
		this.endLongtitude = endLongtitude;
		this.type = type;
		this.objColor = objColor;		
	}
	
	
	/**
	 * This method draw circle,line according to type set
	 * @ objGraphics- Object of type MapGraphics 
	 */
	public void paintLayer( MapGraphics objGraphics ) {
		
		if (type.equals(SGSLConstants.CMD_CIRCLE))
		{
			 drawCircle(objGraphics,false);
		}
		else if (type.equals(SGSLConstants.CMD_LINE))
		{
			drawArc(objGraphics);
		}	
		else if (type.equals(SGSLConstants.CMD_FILLEDCIRCLE))
		{
			drawCircle(objGraphics,true);
		}
		
	}


    /**
     * This method returns latitude
     * @return latitude - Latitude in degree of type float
     */
	public float getLatitude() {
		return startLatitude;
	}


    /**
     * This method sets latitude
     * @param latitude - Latitude in degree of type float
     */
	public void setLatitude(float latitude) {
		this.startLatitude = latitude;
	}


    /**
     * This method sets longtitude
     * @return longtitude - Longtitude in degree of type float
     */
	public float getLongtitude() {
		return startLongtitude;
	}


	 /**
     * This method sets longtitude
     * @param latitude - Longtitude in degree of type float
     */
	public void setLongtitude(float longtitude) {
		this.startLongtitude = longtitude;
	}


    /**
     * 
     * This method returns radius
     * @return Radius - Radius of the circle of type float
     */
	public double getRadius() {
		return radius;
	}


   /**
    * This method sets radius 
    * @param radius - Radius of the circle of type double
    */
	public void setRadius(double radius) {
		this.radius = (float) radius;
	}


    /**
     * This method returns type of object namely line,circle,filled circle as a string 
     * @return type- Type of the object as a string
     */
	public String getType() {
		return type;
	}


    /**
     * This method sets type of object namely line,circle,filled circle as a string 
     * @param type -Type of the object as a string
     */
	public void setType(String type) {
		this.type = type;
	}


    /**
     * This method returns end latitude
     * @return Latitude in degrees of type float
     */
	public float getEndLatitude() {
		return endLatitude;
	}


    /**
     * This method sets end latitude
     * @param endLatitude in degrees of type float
     */
	public void setEndLatitude(float endLatitude) {
		this.endLatitude = endLatitude;
	}


	/**
     * This method returns end longtitude
     * @return longtitude in degrees of type float
     */
	public float getEndLongtitude() {
		return endLongtitude;
	}
	
	
	 /**
     * This method sets end longtitude
     * @param endLongtitude in degrees of type float
     */
	public void setEndLongtitude(float endLongtitude) {
		this.endLongtitude = endLongtitude;
	}
	
	
	/**
	 * This method draws circle of either filled or transparent according to the boolean param
	 * @param objGraphics - Mapgraphics object which holds circle
	 * @param filled - True- To draw filled circle
	 *                  False - To draw hallow circle
	 */
	public void drawCircle( MapGraphics objGraphics,boolean filled)
	{
		Graphics2D g2d = objGraphics.getGraphics2D();		
		Projection projection =objGraphics.getProjection();
		
		ProjectionPainter pp = ProjectionPainter.getProjectionPainter( projection );
		g2d.setPaint( objColor);
		
		GeneralPath gc = new GeneralPath();
		ProjectionPainter.smallCircle( startLongtitude, startLatitude, radius, 180, gc, true );
		gc.closePath();
		
		if (filled)
		{
			pp.drawPath( g2d, gc, objColor,objColor);
		}
		else
		{
			pp.drawPath( g2d, gc, objColor,null);
		}
			
	}
	
	/**
	 * This method draws arc from start latitude,longitude to end latitude,longitude.
	 * @param objGraphics - MapGraphics object which holds arc
	 */
	public void drawArc( MapGraphics objGraphics)
	{
		Graphics2D g2d = objGraphics.getGraphics2D();
        Projection projection =objGraphics.getProjection();		
		ProjectionPainter pp = ProjectionPainter.getProjectionPainter( projection );
		g2d.setPaint(objColor);
		GeneralPath line = new GeneralPath();				
		line.moveTo( startLongtitude,startLatitude);
		
		line.lineTo( endLongtitude,endLatitude );		
	
		Shape arc = pp.projectPath( line, null, false );
		
		if ( arc != null )
			g2d.draw( arc );
		
	}
    
	/**
	 * This method returns color object
	 * @return Color object
	 */
	public Color getObjColor() {
		return objColor;
	}
	
	/**
	 * This method sets color object
	 * @param objColor - Color object to be set to draw
	 */
	public void setObjColor(Color objColor) {
		this.objColor = objColor;
	}

}
